import Vue from "vue";
import * as dayjs from "dayjs";
import Decimal from 'decimal.js';

// 日期保留年月日
Vue.filter('DateFormat', function(amount) {
  let str = '';
  if (amount) {
    str = amount.slice(0, 10);
  }
  return str;
});

Vue.filter('AmountFormat', function(amount) {
  amount = Decimal(+amount || 0).toFixed(2);
  let newAmount = '0.00';
  const reg = /(\d)(?=(\d{3})+\.)/g;
  if (!isNaN(amount)) {
    if (typeof amount === 'number' || typeof amount === 'string') {
      newAmount = parseFloat(amount).toFixed(2).replace(reg, '$1,');
    }
  }
  return newAmount;
});

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  let intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') //将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

Vue.filter('moment', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return dayjs(dataStr).format(pattern)
})

/** 字符串超长截取省略号显示 */
Vue.filter('ellipsis', function (value, vlength = 25) {
  if(!value){
    return "";
  }
  console.log('vlength: '+ vlength);
  if (value.length > vlength) {
    return value.slice(0, vlength) + '...'
  }
  return value
})